#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   params_convert.py
@Time    :   2024/10/31 16:24:44
@Author  :   YcZ
@Version :   1.0
@Contact :   yuchao.zhou@outlook.com
@License :   (C)Copyright 2022-2024, YcZ
@Desc    :   
'''

import argparse
import os

from camera_params_convert import parse_and_convert_intrinsic, parse_and_convert_extrinsic
from lidar_params_convert import parse_and_convert_height, parse_and_convert_lidar_extrinsic

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-d','--directory', type=str, default='./orin_params')
    parser.add_argument('-o','--output', type=str, default='./params_output')
    args = parser.parse_args()

    folder = args.directory
    output_folder = args.output
    camera_folder = os.path.join(output_folder, 'orin_camera_params')
    lidar_folder = os.path.join(output_folder, 'lidar_params')
    if not os.path.exists(camera_folder):
        os.makedirs(camera_folder)
    if not os.path.exists(lidar_folder):
        os.makedirs(lidar_folder)

    for file in os.listdir(folder):
        print("checking",file)
        if file.endswith('.yml') or file.endswith('.yaml'):
            if file.startswith('x3p_camera'):
                if 'intrinsics' in file:
                    print("\t", "convert intrinsic", file)
                    parse_and_convert_intrinsic(os.path.join(folder, file), camera_folder)
                elif 'extrinsics' in file:
                    print("\t", "convert extrinsic", file)
                    parse_and_convert_extrinsic(os.path.join(folder, file), camera_folder)
                else:
                    print("\t", "pass", file)
            elif file.startswith('velodyne'):
                if 'extrinsics' in file:
                    print("\t", "convert extrinsic", file)
                    parse_and_convert_lidar_extrinsic(os.path.join(folder, file), lidar_folder)
                else:
                    parse_and_convert_height(os.path.join(folder, file), lidar_folder)
            else:
                print("\t", "pass", file)
        else:
            print("\t", "pass", file)
            continue
    print("Done")
